

package com.hazelcast.client.impl.operations;

import com.hazelcast.client.Client;
import com.hazelcast.client.impl.ClientDataSerializerHook;
import com.hazelcast.client.impl.ClientEndpointImpl;
import com.hazelcast.client.impl.ClientEngine;
import com.hazelcast.spi.impl.operationservice.ReadonlyOperation;

import java.util.Collection;
import java.util.Map;
import java.util.UUID;

import static com.hazelcast.internal.util.MapUtil.createHashMap;


public class GetConnectedClientsOperation extends AbstractClientOperation implements ReadonlyOperation {

    private Map<UUID, String> clients;

    public GetConnectedClientsOperation() {
    }

    @Override
    public void run() throws Exception {
        ClientEngine service = getService();
        final Collection<Client> serviceClients = service.getClients();
        this.clients = createHashMap(serviceClients.size());
        for (Client clientEndpoint : serviceClients) {
            ClientEndpointImpl clientEndpointImpl = (ClientEndpointImpl) clientEndpoint;
            this.clients.put(clientEndpointImpl.getUuid(), clientEndpointImpl.getClientType());
        }
    }

    @Override
    public Object getResponse() {
        return clients;
    }

    @Override
    public int getClassId() {
        return ClientDataSerializerHook.GET_CONNECTED_CLIENTS;
    }

}
